﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AcDotNetTool;
namespace _06动态交互类DrawJig
{
    public class Class1
    {
        [CommandMethod("MoveDemo",CommandFlags.UsePickSet)]
        public void MoveDemo()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //会接收命令执行前用户选择的图形对象
            PromptSelectionResult psr = ed.SelectImplied();
            //如果执行命令前用户没有选择图形对象,则提示用户选择
            if (psr.Status != PromptStatus.OK)
            {
                psr = ed.GetSelection();
            }
            if (psr.Status != PromptStatus.OK) return;
            //这里获取用户指定的基础点
            Point3d pointBase = new Point3d(0,0,0);
            PromptPointOptions ppo = new PromptPointOptions("\n 指定基点或 [位移(D)] <位移>:");
            ppo.AllowNone = true;
            PromptPointResult ppr =  ed.GetPoint(ppo);
            //判断用户指定基础点的输入
            if (ppr.Status == PromptStatus.Cancel) return;
            if (ppr.Status == PromptStatus.OK) pointBase = ppr.Value;

            //获取选择图形的图形对象
            List<Entity> entList = new List<Entity>();
            ObjectId[] ids = psr.Value.GetObjectIds();
            entList = this.GetEntity(ids);
            //复制图形的,用户传递给MoveJig类的对象
            Matrix3d mt = Matrix3d.Displacement(new Vector3d(0, 0, 0));
            List<Entity> entListCopy = this.CopyEntity(entList,mt);
            List<Entity> entListCopy2 = this.CopyEntity(entList, mt);
            //改变原图形对象的颜色
            this.LowColorEntity(entList,211);
            //声明一个移动类
            MoveJig moveJig = new MoveJig(entListCopy, pointBase);
            PromptResult pr = ed.Drag(moveJig);
            //相应鼠标点击处理
            if (pr.Status == PromptStatus.OK)
            {
                List<Entity> ents = moveJig.GetEntityList();
                db.AddEnityToModelSpace(ents.ToArray());
                this.DeleteEntitys(entList.ToArray());
            }
            //相应ESC处理
            if (pr.Status == PromptStatus.Cancel)
            {
                db.AddEnityToModelSpace(entListCopy2.ToArray());
                this.DeleteEntitys(entList.ToArray());
            }
            //相应回车和空格处理
            if (pr.Status == PromptStatus.Keyword && pr.StringResult == " ") 
            {
                for (int i = 0; i < entListCopy2.Count; i++)
                {
                    entListCopy2[i].MoveEntity(Point3d.Origin, pointBase);
                }
                db.AddEnityToModelSpace(entListCopy2.ToArray());
                this.DeleteEntitys(entList.ToArray());
            }
           
        }

        /// <summary>
        /// 删除图形对象
        /// </summary>
        /// <param name="ents">图形对象数组</param>
        private void DeleteEntitys(Entity[] ents)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                for (int i = 0; i < ents.Length; i++)
                {
                    Entity ent = (Entity)ents[i].ObjectId.GetObject(OpenMode.ForWrite);
                    ent.Erase();
                }
                trans.Commit();
            }
        }
        /// <summary>
        /// 复制图形对象
        /// </summary>
        /// <param name="entList">图形对象集合</param>
        /// <param name="mt">变换矩阵</param>
        /// <returns>List<Entity></returns>
        private List<Entity> CopyEntity(List<Entity> entList,Matrix3d mt)
        {
            List<Entity> entListCopy = new List<Entity>();
            for (int i = 0; i < entList.Count; i++)
            {
                entListCopy.Add(entList[i].GetTransformedCopy(mt));
            }
            return entListCopy;
        }
        /// <summary>
        /// 获取选择集中的图形对象
        /// </summary>
        /// <param name="ids">图形对象的ObjectId集合</param>
        /// <returns>List<Entity></returns>
        private List<Entity> GetEntity(ObjectId[] ids)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            List<Entity> entList = new List<Entity>();
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                for (int i = 0; i < ids.Length; i++)
                {
                    Entity ent = (Entity)ids[i].GetObject(OpenMode.ForRead);
                    entList.Add(ent);
                }
            }
            return entList;
            
        }
        /// <summary>
        /// 改变图形对象的颜色
        /// </summary>
        /// <param name="entList">图形对象</param>
        private void LowColorEntity(List<Entity> entList,int colorIndex)
        {
            Database db = HostApplicationServices.WorkingDatabase;
           
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                for (int i = 0; i < entList.Count; i++)
                {
                    Entity ent = (Entity)entList[i].ObjectId.GetObject(OpenMode.ForWrite);
                    ent.ColorIndex = colorIndex;
                }
                trans.Commit();
            }
            
        }
    }
}
